package top.juntech.msweathercollection.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import top.juntech.msweathercollection.service.WeatherColletionSevice;
import top.juntech.msweathercollection.utils.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class WeatherCollectionServiceImpl implements WeatherColletionSevice {

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Override
    public void syncDateByCityId(String cityId) {

    }

    @Override
    public String saveWeatherData(String uri) {
        log.info("uri: {}",uri);
        String strBody = null;
        ResponseEntity<String> resp = restTemplate.getForEntity(uri, String.class);
        strBody = null;
        if(200 == resp.getStatusCodeValue()){
            try {
                strBody = StringUtils.conventFromGzip(resp.getBody());
            }catch (Exception e){
                throw  new RuntimeException(e);
            }

        }
        redisTemplate.opsForValue().set(uri,strBody,24L, TimeUnit.HOURS);
        return strBody;
    }
}


